package com.humanmonth.home.component.page.htree{
	
	import com.humanmonth.home.component.page.htree.effect.DisplayPropertyEffect;
	import com.humanmonth.home.component.page.htree.layout.Layout;
	import com.humanmonth.util.effect.MoveHelper;
	import com.humanmonth.util.effect.PassbyEffectGrow;
	
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	import mx.effects.Move;
	import mx.effects.easing.Elastic;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;

	public class NodeAs extends Node {
		/**
		 *主键 
		 */
		public var myId:Number;
		/**
		 *排序 
		 */
		public var myOrder:int;
		/**
		 *节点名字 
		 */
		 [Bindable]
		public var myName:String;
		/**
		 *端点集合 
		 */
		private var _points :ArrayCollection;
		/**
		 *所有节点 
		 */
		 [Bindable]
		public static var allNode:ArrayCollection=new ArrayCollection();
		/**
		 *容器 
		 */
		public static var whiteBoard:WhiteBoardAs;
		/**
		 *是否已经放到容器 
		 */
		public var isPlaceToContainer : Boolean = false;
		public function NodeAs(x:Number,y:Number,order:int,name:String,myId:Number=0) {
			if(myId==0){
				this.myId=new Date().getTime();
			}else{
				this.myId=myId;
			}
			this.x=x;
			this.y=y;
			this.myOrder=order;
			this.myName=name;
			new MoveHelper(this);
			allNode.addItem(this);
			new PassbyEffectGrow().applyFilter(this);
			new DisplayPropertyEffect().applyFilter(this);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,myInit);
			whiteBoard=Application.application.hTree.whiteBoard;
			if(whiteBoard!=null){
			whiteBoard.addChild(this);
			}
			this.doubleClickEnabled=true;
		}
		
		private function myInit(event:Event):void{
			BindingUtils.bindProperty(this.step,"text",this,"myName");
			this.setRedraw();
			this.addEventListener(MouseEvent.DOUBLE_CLICK,popEditNode);
			this.initMenu();
			this.toolTip="右击可以删除及修改节点";
		}
		
		/**
		 *初始化菜单 
		 */
		private function initMenu(event:Event=null):void{
			var deleteLineItem : ContextMenuItem = new ContextMenuItem("删除当前节点");
			deleteLineItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, deleteNode);
			var editLineItem : ContextMenuItem = new ContextMenuItem("编辑当前节点");
			editLineItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, popEditNode);
			var contextMenu:ContextMenu=new ContextMenu();
			contextMenu.hideBuiltInItems();
			contextMenu.customItems.push(deleteLineItem,editLineItem);
			this.contextMenu=contextMenu;
		}
		
		/**
		 * 删除节点 
		 */
		private function deleteNode(event:Event):void{
			whiteBoard.removeChild(this);
			var index:int=NodeAs.allNode.getItemIndex(this);
			NodeAs.allNode.removeItemAt(index);
			var lines:Array=new Array();
			for each(var cLine:MyLine in MyLine.allLines){
				if(cLine.startNode==this||cLine.endNode==this){
					lines.push(cLine);
				}
			}
			for each(var linet:MyLine in lines){
				var index2:int=MyLine.allLines.getItemIndex(linet);
				MyLine.allLines.removeItemAt(index2);
				whiteBoard.removeChild(linet);
			}
		}
		
		/**
		 * 弹出编辑节点的的对话框 
		 */
		private function popEditNode(event:Event):void{
			var nodeForm:NodeFormAs=NodeFormAs(PopUpManager.createPopUp(whiteBoard,NodeFormAs,true));
			PopUpManager.centerPopUp(nodeForm);
			nodeForm.setNode(this);
		}
		
		/**
		 *当节点被移动时，重绘线条 
		 */
		private function setRedraw():void{
			BindingUtils.bindSetter(drawRelateLine,this,"x");
			BindingUtils.bindSetter(drawRelateLine,this,"y");				
		}
		
		/**
		 * 调用相关线条的重绘
		 */
		private function drawRelateLine(obj:Object=null):void{
			MyLine.drawRelate(this);
		}
		
		/**
		 * 初始化连线的四个接触点
		 */
		private function initPoints():void {
			this._points=new ArrayCollection();
			//上
			this._points.addItem(new Point(this.x+this.width/2,this.y));
			//下
			this._points.addItem(new Point(this.x+this.width/2,this.y+this.height));
			//左
			this._points.addItem(new Point(this.x,this.y+this.height/2));
			//右
			this._points.addItem(new Point(this.x+this.width,this.y+this.height/2));
		}
		
		public function get points():ArrayCollection{
			this.initPoints();
			return this._points;
		}
		
		/**
		 * 以ID为条件获取对像
		 */
		public static function getNodeById(myId:Number):NodeAs{
			for each (var node:NodeAs in allNode){
				if(node.myId==myId){
					return node;
				}
			}
			return null;
		}
		
		/**
		 * 返回属性表格的数据 
		 */
		public function toTableString():Array{
			return [{myName:"节点名称",myValue:this.myName},{myName:"排序",myValue:this.myOrder},{myName:"X坐标",myValue:this.x},{myName:"Y坐标",myValue:this.y}];
		}
		
		/**
		 * 设置组件的座标 
		 */
		public function setPosition(point : Point) : void {
 		if(!Layout.isVertical){
 			var tY:Number=point.x;
 			point.x=point.y;
 			point.y=tY;
 		}
 			var moveE:Move=new Move();
			moveE.easingFunction=Elastic.easeInOut;
			moveE.xFrom=this.x;
			moveE.yFrom=this.y;
			moveE.xTo=point.x;
			moveE.yTo=point.y;
			moveE.duration=2000;
			moveE.play([this]); 
		}
		
		/**
		 * 把所有步骤的是否已放入容器的状态设为false 
		 */
		public static function setAllIsPlaceToContainerToFalse() : void {
			for each(var node:NodeAs in NodeAs.allNode) {
				node.isPlaceToContainer = false;
			}
		}
		
		/**
		 * 如果node不存在于result中，即加入到result 
		 */
		private static function addStepToResultIfNotExit(node:NodeAs,result : Array) : void {
 			for each(var node2:NodeAs in result) {
				if(node2.myId == node.myId) {
					return;
				}
			}  
			result.push(node);		
		}
		/**
		 *获取下一级节点 
		 */
		private static function getNextStepsByStep(node:NodeAs,result : Array) : void {
			for each(var line:MyLine in MyLine.allLines) {
				if(node.myId == line.startNode.myId) {
					NodeAs.addStepToResultIfNotExit(line.endNode, result);
				}
			}
		}
		
		/** 根据当前的节点数返回下一级别的节点，如果返回值为null，表示跌待完成
		 * @param steps 当前的节点（数组）
		 */
		public static function getNextLevelNodeByNodes(nodes : Array) : Array {
			var result : Array = new Array();
			for each(var node:NodeAs in nodes) {
				NodeAs.getNextStepsByStep(node, result);
			}
			if(result.length > 0) {
				return result;
			}
			for each(var node2:NodeAs in NodeAs.allNode) {
				if(!node2.isPlaceToContainer) {
					return [node2];
				}
			}
			return null;
		}
	}
}